<script setup>
import { ref, onMounted } from 'vue'
import ChartComponent from './components/index.vue'

// 控制全屏状态
const isFullscreen = ref(false)

// 存储线别和工序信息
const lineId = ref('')
const stepName = ref('')
const lineName = ref('')
const processName = ref('')

// 解析URL参数
const parseUrlParams = () => {
  const urlParams = new URLSearchParams(window.location.search)
  lineId.value = urlParams.get('lineId') || ''
  stepName.value = urlParams.get('stepName') || ''
  
  // 转换lineId为中文线别名称
  if (lineId.value) {
    lineName.value = `${lineId.value}线`
  }
  
  // 转换stepName为中文工序名称
  const processMap = {
    'DC': '叠层',
    'HEL': '后道EL'
  }
  processName.value = processMap[stepName.value] || stepName.value
}

// 页面加载时解析参数
onMounted(() => {
  parseUrlParams()
  // 监听URL变化
  window.addEventListener('popstate', parseUrlParams)
})

// 切换全屏模式
const toggleFullscreen = () => {
  if (!document.fullscreenElement) {
    // 进入全屏
    if (document.documentElement.requestFullscreen) {
      document.documentElement.requestFullscreen()
    } else if (document.documentElement.mozRequestFullScreen) {
      // Firefox
      document.documentElement.mozRequestFullScreen()
    } else if (document.documentElement.webkitRequestFullscreen) {
      // Chrome, Safari and Opera
      document.documentElement.webkitRequestFullscreen()
    } else if (document.documentElement.msRequestFullscreen) {
      // IE/Edge
      document.documentElement.msRequestFullscreen()
    }
    isFullscreen.value = true
  } else {
    // 退出全屏
    if (document.exitFullscreen) {
      document.exitFullscreen()
    } else if (document.mozCancelFullScreen) {
      // Firefox
      document.mozCancelFullScreen()
    } else if (document.webkitExitFullscreen) {
      // Chrome, Safari and Opera
      document.webkitExitFullscreen()
    } else if (document.msExitFullscreen) {
      // IE/Edge
      document.msExitFullscreen()
    }
    isFullscreen.value = false
  }
}

// 监听全屏状态变化
const handleFullscreenChange = () => {
  isFullscreen.value = !!document.fullscreenElement
}

// 添加事件监听器
document.addEventListener('fullscreenchange', handleFullscreenChange)
document.addEventListener('webkitfullscreenchange', handleFullscreenChange)
document.addEventListener('mozfullscreenchange', handleFullscreenChange)
document.addEventListener('MSFullscreenChange', handleFullscreenChange)
</script>

<template>
  <div class="app-container" :class="{ 'fullscreen': isFullscreen }">
    <button 
      class="fullscreen-toggle"
      @click="toggleFullscreen"
      :title="isFullscreen ? '退出全屏' : '进入全屏'"
    >
      {{ isFullscreen ? '📱' : '📊' }}
    </button>
    
    <main class="app-main">
      <div class="chart-wrapper">
        <h2>{{ lineName && processName ? `${lineName}${processName}产能趋势图` : '数据趋势图表' }}</h2>
        <ChartComponent />
      </div>
    </main>
  </div>
</template>

<style>
/* 全局样式重置 - 确保页面占满整个屏幕 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  width: 100%;
  height: 100vh;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

#app {
  width: 100%;
  height: 100vh;
  margin: 0;
  padding: 0;
}
</style>

<style scoped>
/* 基础样式 */
.app-container {
  min-height: 100vh;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f5f5;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  position: relative;
  margin: 0;
  padding: 0;
  width: 100%;
}

.app-main {
  flex: 1;
  padding: 0;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  margin: 0;
}

.chart-wrapper {
  background-color: white;
  border-radius: 0;
  box-shadow: none;
  padding: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  box-sizing: border-box;
  width: 100%;
  max-width: none;
  margin: 0;
}

.chart-wrapper h2 {
  margin: 0 0 1.5rem 0;
  color: #2c3e50;
  font-size: 1.4rem;
  font-weight: 500;
  text-align: center;
}

/* 响应式调整 */
@media screen and (max-width: 768px) {
  .app-main {
    padding: 1rem;
  }
  
  .chart-wrapper {
    padding: 1rem;
  }
  
  .chart-wrapper h2 {
    font-size: 1.2rem;
  }
}

@media screen and (max-width: 480px) {
  .app-main {
    padding: 0.5rem;
  }
  
  .chart-wrapper {
    padding: 0.75rem;
    border-radius: 4px;
  }
  
  .chart-wrapper h2 {
    font-size: 1.1rem;
  }
}

/* 全屏模式下的图表样式 */
.app-container.fullscreen .chart-wrapper {
  max-width: 100%;
  max-height: 90%;
  margin: 1rem auto;
  width: 90%;
  height: 80%;
}

/* 全屏按钮样式 */
.fullscreen-toggle {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 1000;
  background-color: rgba(44, 62, 80, 0.9);
  color: white;
  border: none;
  border-radius: 50%;
  width: 3rem;
  height: 3rem;
  font-size: 1.5rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  transition: all 0.3s ease;
  backdrop-filter: blur(5px);
}

.fullscreen-toggle:hover {
  background-color: rgba(44, 62, 80, 1);
  transform: scale(1.1);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.fullscreen-toggle:active {
  transform: scale(0.95);
}

/* 全屏模式样式 */
.app-container.fullscreen {
  background-color: #1a1a1a;
  overflow: hidden;
}

.app-container.fullscreen .app-main {
  padding: 0;
  max-width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-container.fullscreen .chart-wrapper {
  background-color: #1a1a1a;
  border-radius: 0;
  box-shadow: none;
  padding: 0;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  color: white;
}

.app-container.fullscreen .chart-wrapper h2 {
  color: white;
  margin: 1rem 0;
  font-size: 1.5rem;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
  text-align: center;
}

/* 响应式下的全屏按钮调整 */
@media screen and (max-width: 768px) {
  .fullscreen-toggle {
    width: 2.5rem;
    height: 2.5rem;
    font-size: 1.25rem;
    top: 0.75rem;
    right: 0.75rem;
  }
}

@media screen and (max-width: 480px) {
  .fullscreen-toggle {
    width: 2.25rem;
    height: 2.25rem;
    font-size: 1.1rem;
    top: 0.5rem;
    right: 0.5rem;
  }
}

/* 全屏模式下的响应式调整 */
@media screen and (max-width: 768px) {
  .app-container.fullscreen .chart-wrapper h2 {
    font-size: 1.3rem;
    margin-bottom: 0.75rem;
  }
}

@media screen and (max-width: 480px) {
  .app-container.fullscreen .chart-wrapper h2 {
    font-size: 1.1rem;
    margin-bottom: 0.5rem;
  }
}
</style>
